package com.molyfun.parents.activity.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.TimeUtils;
import com.molyfun.parents.view.calendar.CalendarDay;
import com.molyfun.parents.view.calendar.MaterialCalendarView;
import com.molyfun.parents.view.calendar.OnDateSelectedListener;

import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.molyfun.parents.fragment.dialog.DateChooseDialogFragment.FLAG_DATE_CHOOSED;
import static com.molyfun.parents.fragment.dialog.DateChooseDialogFragment.FLAG_DATE_PAGE;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/23/16.
 */
public class ChooseDateActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.black_place)
    LinearLayout mBlackPlace;
    @InjectView(R.id.dateview_container)
    RelativeLayout mDateContainer;

    private MaterialCalendarView mMaterialCalendarView;

    private static final String FLAG_TEAM_ID = "team_id";

    public static final void startActivity(Context context, String date, int currentPage) {
        Intent intent = new Intent();
        intent.setClass(context, ChooseDateActivity.class);
        intent.putExtra(FLAG_DATE_CHOOSED, date);
        intent.putExtra(FLAG_DATE_PAGE, currentPage);
        context.startActivity(intent);
    }

    public static final void startActivityByTeam(Context context, String date, String teamId) {
        Intent intent = new Intent();
        intent.setClass(context, ChooseDateActivity.class);
        intent.putExtra(FLAG_DATE_CHOOSED, date);
        intent.putExtra(FLAG_TEAM_ID, teamId);
        intent.putExtra(FLAG_DATE_PAGE, MaterialCalendarView.PageIdentify.PageTeamCourse);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_date);
        ButterKnife.inject(this);

        mBlackPlace.setOnClickListener(this);


        String choosedDate = getIntent().getStringExtra(FLAG_DATE_CHOOSED);

        if(!TextUtils.isEmpty(choosedDate)){
            Date date = TimeUtils.getDateByStr(choosedDate);
            String teamId = getIntent().getStringExtra(FLAG_TEAM_ID);
            if(TextUtils.isEmpty(teamId)){
                mMaterialCalendarView = new MaterialCalendarView(this, getIntent().getIntExtra(FLAG_DATE_PAGE, MaterialCalendarView.PageIdentify.PageCourse));//(MaterialCalendarView)view.findViewById(R.id.calendarView);
            }else{
                mMaterialCalendarView = new MaterialCalendarView(this, teamId);//(MaterialCalendarView)view.findViewById(R.id.calendarView);
            }
            mMaterialCalendarView.setCurrentDate(date);
            mMaterialCalendarView.setDateSelected(date, true);
            mMaterialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
                @Override
                public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
//                    if(mDateChooseDialogClickListener != null){
//                        mDateChooseDialogClickListener.onDateChange(formatDate(date.getYear(), date.getMonth() + 1, date.getDay()));
//                    }

                    Message uimessage = new Message();
                    uimessage.what = EventDispatcherEnum.UI_EVENT_DATE_CHANGE;
                    uimessage.obj = formatDate(date.getYear(), date.getMonth() + 1, date.getDay());
                    MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);

                    finish();
//                    getDialog().dismiss();
                }
            });

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0 , getResources().getDimensionPixelOffset(R.dimen.common_normal_padding) , 0);
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            mDateContainer.addView(mMaterialCalendarView, layoutParams);
        }

    }

    private String formatDate(int year, int month, int day){
        StringBuilder sb = new StringBuilder();
        sb.append(year);
        sb.append("-");
        if(month > 9){
            sb.append(month + "-");
        }else{
            sb.append("0" + month + "-");
        }
        if(day > 9){
            sb.append(day);
        }else{
            sb.append("0" + day);
        }
        return sb.toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.black_place:
                finish();
                break;
            default:
                break;
        }
    }

}